package com.wjh.controler;

import com.wjh.bean.Student;
import com.wjh.bean.Student1;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Student1Controler {
    @RequestMapping(value = "/addStudent", method = RequestMethod.GET)
    public ModelAndView student() {
        return new ModelAndView("addStudent", "command", new Student1());
    }

    @ModelAttribute("student")
    public Student1 createStudentModel() {
        return new Student1();
    }

    @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
    public String addStudent(@ModelAttribute("student") @Validated Student1 student,
                             BindingResult bindingResult, Model model) {
        if (bindingResult.hasErrors()) {
            return "addStudent";
        }
        model.addAttribute("name", student.getName());
        model.addAttribute("age", student.getAge());
        model.addAttribute("id", student.getId());
        model.addAttribute("student",student);
        return "result";
    }
}